package com.qd.push.log;

import com.qd.push.model.HkPushLog;
import com.qd.push.model.HkPushReq;
import com.qd.push.model.HkPushResp;
import com.qd.push.service.HkPushLogService;
import com.qd.push.service.HkPushReqService;
import com.qd.push.service.HkPushRespService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;


/**
 * @author sjk
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class SendLogsService {
    private final HkPushLogService pushLogService;
    private final HkPushReqService pushReqService;
    private final HkPushRespService pushRespService;

    @Async("asyncExecutor")
    @Transactional(rollbackFor = Exception.class)
    public void saveSendLog(HkPushLog hkPushLog, HkPushReq hkPushReq, HkPushResp hkPushResp) {
        pushLogService.save(hkPushLog);
        if (null != hkPushResp) {
            hkPushResp.setLogId(hkPushLog.getLogId());
            pushRespService.save(hkPushResp);
            hkPushResp.freeData();
        }
        if (null != hkPushReq && StringUtils.hasLength(hkPushReq.getReqParam())) {
            hkPushReq.setLogId(hkPushLog.getLogId());
            pushReqService.save(hkPushReq);
            hkPushReq.freeData();
        }
        hkPushLog.freeData();
    }
}
